package com.project.xy.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.project.common.entity.PageVo;
import com.project.common.utils.Serial;
import com.project.xy.entity.DFile;
import com.project.xy.entity.DesignModule;
import com.project.xy.entity.ProSearch;
import com.project.xy.mapper.DModuleDetailsMapper;
import com.project.xy.mapper.DesignModuleMapper;
import com.project.xy.service.DFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/dFile")
public class DFileController {

    @Autowired
    private DFileService dFileService;

    @Autowired
    private DesignModuleMapper designModuleMapper;

    @Autowired
    private DModuleDetailsMapper dModuleDetailsMapper;

    //查询审核通过并且类型为商品的产品档案
    @PostMapping("/queryDFile.action")
    @ResponseBody
    public PageVo queryDFile(@RequestBody Map<String,String> map){
        Page<Object> objects = PageHelper.startPage(Integer.valueOf(map.get("pageNum")),Integer.valueOf(map.get("pageSize")));
        List<DFile> dFiles = dFileService.queryDFile();
        PageInfo pageInfo = new PageInfo(dFiles);
//        System.out.println(pageInfo.getList());
        PageVo pageVo = new PageVo(pageInfo.getTotal(),pageInfo.getList());
//        System.out.println(pageVo);
        return pageVo;
    }

    @PostMapping("/queryConfigurationSheet.action")
    @ResponseBody
    public PageVo<DFile> SearchCheck_tagAndDesign_cell_tag(@RequestBody Map<String,String> map){

        PageVo<DFile> pageVo = dFileService.SearchCheck_tagAndDesign_cell_tag(map);

//        System.out.println(pageVo.getRows());

        return pageVo;
    }
    @PostMapping("/queryDFileCheckAllOk")
    @ResponseBody
    public PageVo<DFile> queryDFileCheckAllOk(@RequestBody Map<String,String> map){

        PageVo<DFile> pageVo = dFileService.queryDFileCheckAllOk(map);

        return pageVo;
    }

//    添加产品档案
    @PostMapping("/addProduct")
    @ResponseBody
    public Integer addProduct(DFile dFile, String firstName,String secondName,String thirdName) throws IOException {

        dFile.setREAL_COST_PRICE(dFile.getCOST_PRICE());
        dFile.setCHECK_TAG("S001-0");
        dFile.setCHANGE_TAG("D002-0");
        dFile.setPRICE_CHANGE_TAG("J001-0");
        dFile.setDELETE_TAG("C001-0");
        dFile.setDESIGN_MODULE_TAG("W001-0");
        dFile.setDESIGN_PROCEDURE_TAG("G001-0");
        dFile.setDESIGN_CELL_TAG("K001-0");
        dFile.setFIRST_KIND_NAME(firstName.substring(3));
        dFile.setSECOND_KIND_NAME(secondName.substring(3));
        dFile.setTHIRD_KIND_NAME(thirdName.substring(3));
        dFile.setFIRST_KIND_ID(firstName.substring(0,2));
        dFile.setSECOND_KIND_ID(secondName.substring(0,2));
        dFile.setTHIRD_KIND_ID(thirdName.substring(0,2));
        Serial serial = new Serial();
        serial.addUser("PRODUCT_ID","100");
        String product_id = serial.getNumber("PRODUCT_ID", "100", dFile.getFIRST_KIND_ID(), dFile.getSECOND_KIND_ID(), dFile.getTHIRD_KIND_ID());
        dFile.setPRODUCT_ID(product_id);
//        System.out.println(dFile.toString());
        int insert = dFileService.insert(dFile);

        return insert;
    }

//    根据审批状态查询
    @PostMapping("/selectByConState.action")
    @ResponseBody
    public List<DFile> selectByConState(String state){
        List<DFile> dFiles = dFileService.selectByComState(state);
        return dFiles;
    }

    @GetMapping("/checkById")
    @ResponseBody
    public DFile checkById(Integer id){
        DFile dFile = dFileService.selectByPrimaryKey(id);
        return dFile;
    }

    @GetMapping("/adoptComposite.action")
    @ResponseBody
    public Integer adoptComposite(DFile dFile){

        dFile.setCHECK_TAG("S001-1");
        int i = dFileService.updateByPrimaryKeySelective(dFile);
        return i;
    }

    @PostMapping("/selectByCheck.action")
    @ResponseBody
    public List<DFile> selectByCheck(ProSearch search){
        List<DFile> dFiles = dFileService.selectByCheck(search);
        return dFiles;
    }
    @PostMapping("/selectByDelete.action")
    @ResponseBody
    public List<DFile> selectByDelete(String tag){
        List<DFile> dFiles = dFileService.selectByDelete(tag);
//        System.out.println(dFiles.toString());
        return dFiles;
    }
    @GetMapping("/changeDelete")
    @ResponseBody
    public Integer changeDelete(Integer id,String tag){
        DFile dFile = dFileService.selectByPrimaryKey(id);
        dFile.setDELETE_TAG(tag);
        if(tag.equals("C001-1")){
            DesignModule designModule = designModuleMapper.selectByProdId(dFile.getPRODUCT_ID());
            if (designModule!=null){
                dFile.setDESIGN_MODULE_TAG("W001-0");
                dModuleDetailsMapper.deleteByPid(designModule.getId());
                designModuleMapper.deleteByPrimaryKey(designModule.getId());
            }
        }
        int i = dFileService.updateByPrimaryKey(dFile);
        return i;
    }

    @GetMapping("/deleteById")
    @ResponseBody
    public Integer deleteById(Integer id){
        DFile dFile = dFileService.selectByPrimaryKey(id);
        DesignModule designModule = designModuleMapper.selectByPrimaryKey(id);
        if (designModule!=null){
            dFile.setDESIGN_MODULE_TAG("W001-0");
            dModuleDetailsMapper.deleteByPid(designModule.getId());
            designModuleMapper.deleteByPrimaryKey(designModule.getId());
        }
        dFileService.updateByPrimaryKey(dFile);
        int i = dFileService.deleteByPrimaryKey(id);
        return i;
    }

    @GetMapping("/materielSelect.action")
    @ResponseBody
    public List<DFile> materielSelect1(String type){
        List<DFile> dFiles = dFileService.materielSelect(type);
        return dFiles;
    }



    @PostMapping("/selectByPID.action")
    @ResponseBody
    public DFile selectByPIDAction(String id){
        DFile dFile = dFileService.selectByPID(id);
        return dFile;
    }

    @PostMapping("/selectByPID")
    @ResponseBody
    public DFile selectByPID(String id){
        DFile dFile = dFileService.selectByPID(id);
        return dFile;
    }

    @PostMapping("/queryRegisterAll.action")
    @ResponseBody
    public PageVo<DFile> queryRegisterAll(@RequestBody Map<String,String> map){
        return dFileService.queryRegisterAll(map);
    }
    @PostMapping("/updProduct.action")
    @ResponseBody
    public Integer updProduct(DFile dFile){

        DFile file = dFileService.selectByPID(dFile.getPRODUCT_ID());
        DesignModule designModule = designModuleMapper.selectByProdId(dFile.getPRODUCT_ID());
        if (designModule!=null){
            dFile.setDESIGN_MODULE_TAG("W001-0");
            dModuleDetailsMapper.deleteByPid(designModule.getId());
            designModuleMapper.deleteByPrimaryKey(designModule.getId());
        }

        dFile.setCHANGE_TAG("D002-1");
        Integer count = file.getFILE_CHANGE_AMOUNT();
        dFile.setFILE_CHANGE_AMOUNT(count==null?1:count+1);
        dFile.setCHECK_TAG("S001-0");
        dFile.setTYPE(dFile.getTYPE().equals("商品")?"Y001-1":"Y001-2");
//        System.out.println(dFile.toString());

        int i = dFileService.updateByPrimaryKeySelective(dFile);
        return i;
    }



    @PostMapping("/queryOutRegisterAll.action")
    @ResponseBody
    public PageVo<DFile> queryOutRegisterAll(@RequestBody Map<String,String> map){
//        System.out.println(dFileService.queryOutRegisterAll(map));
        return dFileService.queryOutRegisterAll(map);
    }

}

